<?php

/*
 * Class that operate on table 'conteudocampo'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2009-08-25 19:00
 */
class ConteudoCampoDAO {

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return ConteudoCampoMySql
	 */
	public function load($id){
		$sql = 'SELECT * FROM conteudocampo WHERE codCampo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM conteudocampo';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}

	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM formulario ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}

	/**
 	 * Delete record from table
 	 * @param conteudocampo primary key
 	 */
	public function delete($codFormulario){
		$sql = 'DELETE FROM conteudocampo WHERE codCampo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($codFormulario);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Insert record to table
 	 *
 	 * @param ConteudoCampoMySql conteudocampo
 	 */
	public function insert($conteudocampo){
		$sql = 'INSERT INTO conteudocampo (cpf, conteudo, codCampo) VALUES (?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);

		$sqlQuery->set($conteudocampo->cpf);
		$sqlQuery->set($conteudocampo->conteudo);
		$sqlQuery->set($conteudocampo->codCampo);

		$this->executeInsert($sqlQuery);
	}

	/**
 	 * Update record in table
 	 *
 	 * @param ConteudoCampoMySql conteudocampo
 	 */
	public function update($conteudocampo){
		$sql = 'UPDATE conteudocampo SET conteudo = ? WHERE codCampo = ? AND cpf = ?';
		$sqlQuery = new SqlQuery($sql);

		$sqlQuery->set($conteudocampo->conteudo);
		$sqlQuery->setNumber($conteudocampo->codCampo);
		$sqlQuery->set($conteudocampo->cpf);

		return $this->executeUpdate($sqlQuery);
	}

	/**
	 * Read row
	 *
	 * @return ConteudoCampoMySql
	 */
	protected function readRow($row) {
		$conteudocampo = new ConteudoCampo();

		$conteudocampo->cpf = $row['cpf'];
		$conteudocampo->codCampo = $row['codCampo'];
		$conteudocampo->conteudo = $row['conteudo'];
		
		return $conteudocampo;
	}
	
	/**
	 * 
	 */
	protected function getList($sqlQuery) {
		$tab = QueryExecutor :: execute($sqlQuery);
		$ret = array ();
		for ($i = 0; $i < count($tab); $i++) {
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}

	/**
	 * Get row
	 *
	 * @return ConteudoCampoMySql
	 */
	protected function getRow($sqlQuery) {
		$tab = QueryExecutor :: execute($sqlQuery);
		return $this->readRow($tab[0]);
	}

	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery) {
		return QueryExecutor :: execute($sqlQuery);
	}

	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery) {
		return QueryExecutor :: executeUpdate($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery) {
		return QueryExecutor :: executeInsert($sqlQuery);
	}
		
}
?>